package com.huang.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.eduservice.client.UcenterClient;
import com.huang.eduservice.entity.EduComment;
import com.huang.eduservice.service.EduCommentService;
import com.huang.servicebase.execptionhandler.huangException;
import com.hunag.utils.JwtUtils;
import com.hunag.utils.R;
import com.hunag.utils.orderVo.UcenterOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-03
 */
@RestController
@RequestMapping("/eduservice/edu-comment")
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterClient ucenterClient;

    @PostMapping("getComment/{current}/{limit}/{courseId}")
    public R getComment(@PathVariable long current,@PathVariable long limit,@PathVariable String courseId){
        Page<EduComment> commentPage = new Page<>(current,limit);
             Map<String,Object> map= commentService.getCommentPage(commentPage,courseId);
        return R.ok().data(map);
    }

    @PostMapping("addComment")
    public R addComment(@RequestBody EduComment eduComment, HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterOrder ucenter = ucenterClient.getUcenter(id);
        if(!StringUtils.isEmpty(id)){
            commentService.save(eduComment);

            eduComment.setNickname(ucenter.getNickname());
            eduComment.setAvatar(ucenter.getAvatar());
            return R.ok();
        }else{
            throw new huangException(20001,"当前没有用户，亲先登录");
        }
    }

}

